<?php

/**
 * @author Argel Cabalag
 * @copyright 2009
 */

class pxActions extends sfActions
{
	
	public function executeIndex(sfWebRequest $request)
  {
  	$this->form = new SearchForm();

    $p = Doctrine_Query::create()
    ->from('Person p')
		->where('p.type = ?', 'PX')
    ->orderBy('p.created_at DESC')
    ->limit('10');
    $this->px_list = $p->execute();

  }
 public function executeSearch(sfWebRequest $request)
  {
	  $this->form = new SearchForm();

	  $s = $this->getRequestParameter('list');
	  $this->s = $s;
	  $query = '%'.$this->getRequestParameter('query').'%';

	  if($s == 'MC')
	  {
		  $m = Doctrine_Query::create()
		  ->from('Client c')
		  ->where('c.name LIKE ?', $query);
		  $this->list = $m->execute();
	  }
	  elseif($s == 'PX')
	  {

		  $mm = Doctrine_Query::create()
		      ->select('px.*, p.*')
              ->from('Passenger px, px.Person p')
              ->where('p.last_name LIKE ?', $query)
              ->orWhere('p.first_name LIKE ?', $query);

			//->from('Passenger p')
			//->where('EXISTS (SELECT px.* FROM Person px WHERE px.id = p.person_id and px.first_name LIKE ? $query )');

		  $this->list2 = $mm->execute();			
	  }	  
	}
	
	
	
	
  public function executeShow(sfWebRequest $request)
  {
    $this->px = Doctrine::getTable('Person')->find(array($request->getParameter('id')));
    $this->forward404Unless($this->px);
    $cid = $this->getRequestParameter('id');
    $s = Doctrine_Query::create()
    ->from('Address w')
    ->where('w.person_id ='.$cid.'');
    $this->add_list = $s->execute();
    $t = Doctrine_Query::create()
    ->from('Phone p')
    ->where('p.person_id ='.$cid.'');
    $this->phone_list = $t->execute();
    
  }
  public function executeNew(sfWebRequest $request)
  {
		$client_id = $this->getRequestParameter('client_id');
		$px = new Person();
		$px->setType('PX');
		$px->setClientId($client_id);
		$px->setpx_no($this->computePXNo($client_id));
		$px->setAuthor($this->getUser()->getGuardUser()->getId());
    $this->form = new PxForm($px);







  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod('post'));

		$px = new Person();
		$px->setType('PX');

    $this->form = new PxForm($px);

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($px = Doctrine::getTable('Person')->find(array($request->getParameter('id'))), sprintf('Object Person does not exist (%s).', array($request->getParameter('id'))));
		$px->setType('PX');
		$this->px_id = $px->getId();
    $this->form = new PxForm($px);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod('post') || $request->isMethod('put'));
    $this->forward404Unless($px = Doctrine::getTable('Person')->find(array($request->getParameter('id'))), sprintf('Object client does not exist (%s).', array($request->getParameter('id'))));
		$px->setType('PX');
    $this->form = new PxForm($px);

    $this->processForm($request, $this->form);
		$this->px_id = $px->getId();
    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($px = Doctrine::getTable('Person')->find(array($request->getParameter('id'))), sprintf('Object person does not exist (%s).', array($request->getParameter('id'))));
	  $this->client_id = $px->getid();
    $px->delete();

    $this->redirect('main/index');
  }



  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()));
    if ($form->isValid())
    {
      $px = $form->save();

      $this->redirect('px/edit?id='.$px['id']);
    }
  }

	public function computePXNo($client_id)
	{
		$controlArray = array(0);
		$client = Doctrine::getTable('Client')->find($client_id);
		$persons = $client->getPerson();                           
		$amount = 0;
		if((isset($persons) && $persons != '')){
			foreach($persons as $person)
			{
				$px_no = $person->getpx_no();
				$control_no = (int)substr($px_no, 7, 5);
				array_push($controlArray, $control_no);
				$this->logMessage('values'.$control_no);
			}
			sort($controlArray);
			$index = count($controlArray);
				$this->logMessage('index'.$index);
			$highestControl = $controlArray[$index-1];
				$this->logMessage('control'.$highestControl);
		}
		$amount = $highestControl +1;
		
		$modifier = '';
		if ($amount<=9) {
			$modifier = '0000';
		}else	if($amount>=10 && $amount<=99){
			$modifier = '000';
		}else if ($amount>=100 && $amount<=999){
			$modifier = '00';
		}else if ($amount>=1000 && $amount<=9999){
			$modifier = '0';
		}else if ($amount>=10000 && $amount<=99999){
			$modifier = '';
		}else if ($amount>=100000 && $amount<=999999){
			$modifier = '';
		}

		$mc_control = substr($client->getmc_no(), 0, 6);
		$mc_date = substr($client->getmc_no(), 7, 4);
		return $mc_control.'-'.$modifier.$amount.'-'.$mc_date;
		
	}


}

?>